package com.ruoyi.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ISysConfigService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@RestController
@RequestMapping("/system/config")
public class SysConfigController extends BaseController {
    @Autowired
    ISysConfigService iSysConfigService;
    @GetMapping("/list")//主頁面查詢
    public TableDataInfo you(SysConfig sysConfig){
        List<SysConfig> sysConfigs = iSysConfigService.selectConfigList(sysConfig);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(sysConfigs);
        tableDataInfo.setTotal(new PageInfo(sysConfigs).getTotal());
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查詢");
        return getDataTable(sysConfigs);
    }
    @PostMapping//新增参数设置
    public Integer jk(@Validated @RequestBody SysConfig sysConfig){
       sysConfig.setCreateBy(getUsername());
        int i = iSysConfigService.insertConfig(sysConfig);
        return i;
    }
    @DeleteMapping("/{configId}")//删除参数设置
    public void la(@PathVariable Long[] configId){
        iSysConfigService.deleteConfigByIds(configId);
    }
    @GetMapping("/{configId}")
    public AjaxResult mm(@PathVariable Long configId){
        SysConfig sysConfig = iSysConfigService.selectConfigById(configId);
        return AjaxResult.success(sysConfig);
    }
    @PutMapping
    public Integer o(@Validated @RequestBody SysConfig sysConfig){
      sysConfig.setCreateBy(getUsername());
        int i = iSysConfigService.updateConfig(sysConfig);
        return i;
    }

    @DeleteMapping("/refreshCache")
    public AjaxResult aka(){
        iSysConfigService.resetConfigCache();
        return AjaxResult.success();
    }



    @PostMapping("/export")//文件上传
    public void export(HttpServletResponse response, SysConfig SysConfig) throws Exception{
        List<SysConfig> lista=iSysConfigService.selectConfigList(SysConfig);
        exec(lista,response);
    }

    public void exec(List<SysConfig>SysConfig, HttpServletResponse response) throws Exception {
        XSSFWorkbook xssfWorkbook=new XSSFWorkbook();
        XSSFSheet sheet = xssfWorkbook.createSheet("岗位");
        XSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("参数主键");
        row.createCell(1).setCellValue("参数名称");
        row.createCell(2).setCellValue("系统内置");
        row.createCell(3).setCellValue("参数键值");
        row.createCell(4).setCellValue("参数键名");
        row.createCell(5).setCellValue("创建时间");
        for (int i = 0; i < SysConfig.size(); i++) {
            SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
            Date d = sdf.parse(SysConfig.get(i).getCreateTime().toString());
            SimpleDateFormat formatDatee = new SimpleDateFormat("yyyy-MM-dd");
            String formatDate=formatDatee.format(d);


            XSSFRow row1 = sheet.createRow(i+1);
            row1.createCell(0).setCellValue(SysConfig.get(i).getConfigId());
            row1.createCell(1).setCellValue(SysConfig.get(i).getConfigName());
            row1.createCell(2).setCellValue(SysConfig.get(i).getConfigType());
            row1.createCell(3).setCellValue(SysConfig.get(i).getConfigValue());
            row1.createCell(4).setCellValue(SysConfig.get(i).getConfigKey());
            row1.createCell(5).setCellValue(formatDate);
        }
        ServletOutputStream outputStream = response.getOutputStream();
        xssfWorkbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
        xssfWorkbook.close();
    }
}
